// CommentController.java
package com.csxy.six.controller;

import com.csxy.six.dto.CommentDTO;
import com.csxy.six.service.LostFoundCommentService;
import com.csxy.six.util.Result;
import com.csxy.six.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private LostFoundCommentService commentService;

    @GetMapping
    public Result<List<CommentDTO>> getCommentsByItem(
            @RequestParam Long itemId,
            @RequestParam Integer itemType) {
        return Result.success(commentService.getCommentsByItem(itemId, itemType));
    }

    @PostMapping
    public Result<Long> addComment(@RequestBody CommentDTO commentDTO) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(commentService.addComment(commentDTO, userId));
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteComment(@PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (commentService.deleteComment(id, userId)) {
            return Result.success();
        }
        return Result.fail("删除失败");
    }
}
